<?php
namespace app\admin\controller;

class Note extends Common
{
    public function note_list()
    {
        header("Content-Type:text/html;charset=utf8");
        $admin_id = session("admin_id");
        $ret = db("admin")->where("admin_id={$admin_id}")->find();
        $admin_power = $ret['admin_power'];
        $ret = db('note')->alias('n')->join('user u','n.user_id=u.user_id')->join('plate p','n.plate_id = p.plate_id')
        ->field('n.*,u.user_name,p.plate_name')->order("addtime desc")->paginate(4);
        //var_dump($ret);die;
        $pageHtml = $ret->render();
        $this->assign('ret',$ret);
        $this->assign('admin_power',$admin_power);
        $this->assign('pageHtml',$pageHtml);
        $this->assign('pick',3);
        return view('note_list');
    }

    pubLic function note_pass()
    {
        $note_id = input('get.note_id');
        $ret = db('note')->where("note_id={$note_id}")->find();
        if($ret['note_status']== '1'){
            return [
                'msg'    => '已是启用状态！',
                'status' => 0
            ];
        }
        else
        {
            $ret = db('note')->where("note_id={$note_id}")->update(['note_status'=>'1']);
            if ( $ret == false )
            {
                return [
                    'msg'    => '更改失败',
                    'status' => 0
                ];
            }
            return [
                'msg'    => '更改成功',
                'status' => 1
            ];
        }
    }
    pubLic function note_fail()
    {
        $note_id = input('get.note_id');
        $ret = db('note')->where("note_id={$note_id}")->find();
        if($ret['note_status']== '0')
        {
            return [
                'msg'    => '已是禁用状态！',
                'status' => 0
            ];
        }
        else
        {
            $ret = db('note')->where("note_id={$note_id}")->update(['note_status'=>'0']);
            if ( $ret == false )
            {
                return [
                    'msg'    => '更改失败',
                    'status' => 0
                ];
            }
            return [
                'msg'    => '更改成功',
                'status' => 1
            ];
        }
    }

    public function note_del()
    {
        $note_id = input('get.note_id');
        $ret = db('note')->where("note_id={$note_id}")->delete();
        if(!$ret)
        {
            return [
                'msg' => '删除失败',
                'status' => 0
            ];
        }
        return [
            'msg' => '删除成功',
            'status' => 1
        ];
    }

    public function note_comment()
    {
        header("Content-Type:text/html;charset=utf8");
        $result = db('comment')->alias('c')->join('user u','c.user_id=u.user_id')
        ->join('note n','c.note_id=n.note_id')
        ->field('c.*,u.user_name,n.note_title')->paginate(6);
        $admin_id = session("admin_id");
        $ret = db("admin")->where("admin_id={$admin_id}")->find();
        $admin_power = $ret['admin_power'];
        $this->assign('admin_power',$admin_power);
        $pageHtml = $result->render();
        $this->assign('result',$result);
        $this->assign('pageHtml',$pageHtml);
        return view('comment_list');
    }

    pubLic function comment_pass()
    {
        $comment_id = input('get.id');
        $ret = db('comment')->where("comment_id={$comment_id}")->find();
        if($ret['comment_status']== '1')
        {
            $this->success("已是通过状态！");
        }
        else
        {
            $ret = db('comment')->where("comment_id={$comment_id}")->update(['comment_status'=>'1']);
            if ( $ret == false )
            {
                $this->error("通过失败");
            }
            $this->success("接受成功！","/admin/note/note_comment");
        }
    }

      pubLic function comment_fail()
    {
        $comment_id = input('get.id');
        $ret = db('comment')->where("comment_id={$comment_id}")->find();
        if($ret['comment_status']== '0')
        {
            $this->success("已是不合格状态！");
        }
        else
        {
            $ret = db('comment')->where("comment_id={$comment_id}")->update(['comment_status'=>'0']);
            if ( $ret == false )
            {
                $this->error("拒绝失败");
            }
            $this->success("拒绝成功！","/admin/note/note_comment");
        }
    }

    public function comment_del()
    {
        $comment_id = input('get.id');
        $ret = db('comment')->where("comment_id={$comment_id}")->delete();
        if(!$ret)
        {
            $this->error("删除失败");
        }
            $this->success("删除成功","/admin/note/note_comment");
    }

    public function note_replycomment()
    {
        header("Content-Type:text/html;charset=utf8");
        $result = db('replycomment')->alias('r')
        ->join('user u','r.user_id=u.user_id')
        ->join('note n','r.note_id=n.note_id')
        ->join('comment c','r.comment_id=c.comment_id')
        ->field('r.*,u.user_name,n.note_title,c.comment_content')->paginate(6);
        $admin_id = session("admin_id");
        $ret = db("admin")->where("admin_id={$admin_id}")->find();
        $admin_power = $ret['admin_power'];
        $this->assign('admin_power',$admin_power);
        $pageHtml = $result->render();
        $this->assign('result',$result);
        $this->assign('pageHtml',$pageHtml);    
        return view('replycomment_list');
    }

    pubLic function replycomment_pass()
    {
        $reply_id = input('get.id');
        $ret = db('replycomment')->where("reply_id={$reply_id}")->find();
        if($ret['reply_status']== '1')
        {
            $this->success("已是通过状态！");
        }
        else
        {
            $ret = db('replycomment')->where("reply_id={$reply_id}")->update(['reply_status'=>'1']);
            if ( $ret == false )
            {
                $this->error("通过失败");
            }
            $this->success("接受成功！","/admin/note/note_replycomment");
        }
    }

    pubLic function replycomment_fail()
    {
        $reply_id = input('get.id');
        $ret = db('replycomment')->where("reply_id={$reply_id}")->find();
        if($ret['reply_status']== '0')
        {
            $this->success("已是不合格状态！");
        }
        else
        {
            $ret = db('replycomment')->where("reply_id={$reply_id}")->update(['reply_status'=>'0']);
            if ( $ret == false )
            {
                $this->error("拒绝失败");
            }
            $this->success("拒绝成功！","/admin/note/note_replycomment");
        }
    }

    public function replycomment_del()
    {
        $reply_id = input('get.id');
        $ret = db('replycomment')->where("reply_id={$reply_id}")->delete();
        if(!$ret)
        {
            $this->error("删除失败");
        }
            $this->success("删除成功","/admin/note/note_replycomment");
    }

    public function search_pass()
    {
        $data = db("note")->alias('n')->join("user u","n.user_id =u.user_id")->where("note_status = 1")->select();
        if( empty($data) )
        {
            return 
            ['code'=>0,'msg'=>"查询失败"];
        }
        else
        {
        $this->assign('data',$data);
        $html = $this->fetch('note_liebiao');
        $this->success("成功","",$html);
        }
    }

    public function search_hot()
    {
        $data = db("note")->alias('n')->join("user u","n.user_id =u.user_id")->where("note_status = 2")->select();
        if( empty($data) )
        {
            return 
            ['code'=>0,'msg'=>"查询失败"];
        }
        else
        {
        $this->assign('data',$data);     
        $html = $this->fetch('note_liebiao');
        $this->success("成功","",$html);
        }
    }

    public function search_fail()
    {
        $data = db("note")->alias('n')->join("user u","n.user_id =u.user_id")->where("note_status = 0")->select();
        if( empty($data) )
        {
            return 
            ['code'=>0,'msg'=>"查询失败"];
        }
        else
        {
        $this->assign('data',$data);   
        $html = $this->fetch('note_liebiao');
        $this->success("成功","",$html);
        }
    }

    public function search_replypass()
    {
        $data = db("replycomment")->alias('r')->join("user u","r.user_id =u.user_id")->join('note n','r.note_id=n.note_id')->join('comment c','r.comment_id=c.comment_id')->field('r.*,u.user_name,n.note_title,c.comment_content')->where("reply_status = 1")->select();
        
        if( empty($data) )
        {
            return 
            ['code'=>0,'msg'=>"查询失败"];
        }
        else
        {
        $this->assign('data',$data);
        $html = $this->fetch('replycomment_liebiao');
        $this->success("成功","",$html);
        }
    }

    public function search_replystay()
    {
        $data = db("replycomment")->alias('r')->join("user u","r.user_id =u.user_id")->join('note n','r.note_id=n.note_id')->join('comment c','r.comment_id=c.comment_id')->field('r.*,u.user_name,n.note_title,c.comment_content')->where("reply_status = 2")->select();
        if( empty($data) )
        {
            return 
            ['code'=>0,'msg'=>"查询失败"];
        }
        else
        {
        $this->assign('data',$data);     
        $html = $this->fetch('replycomment_liebiao');
        $this->success("成功","",$html);
        }
    }

    public function search_replyfail()
    {
        $data = db("replycomment")->alias('r')->join("user u","r.user_id =u.user_id")->join('note n','r.note_id=n.note_id')->join('comment c','r.comment_id=c.comment_id')->field('r.*,u.user_name,n.note_title,c.comment_content')->where("reply_status = 0")->select();
        if( empty($data) )
        {
            return 
            ['code'=>0,'msg'=>"查询失败"];
        }
        else
        {
        $this->assign('data',$data);   
        $html = $this->fetch('replycomment_liebiao');
        $this->success("成功","",$html);
        }
    }



    public function search_commentpass()
    {
        $data = db('comment')->alias('c')->join('user u','c.user_id=u.user_id')
        ->join('note n','c.note_id=n.note_id')
        ->field('c.*,u.user_name,n.note_title')->where("comment_status = 1")->select();
        if( empty($data) )
        {
            return 
            ['code'=>0,'msg'=>"查询失败"];
        }
        else
        {
        $this->assign('data',$data);
        $html = $this->fetch('comment_liebiao');
        $this->success("成功","",$html);
        }
    }

    public function search_commentstay()
    {
       $data = db('comment')->alias('c')->join('user u','c.user_id=u.user_id')
        ->join('note n','c.note_id=n.note_id')
        ->field('c.*,u.user_name,n.note_title')->where("comment_status = 2")->select();
        if( empty($data) )
        {
            return 
            ['code'=>0,'msg'=>"查询失败"];
        }
        else
        {
        $this->assign('data',$data);     
        $html = $this->fetch('comment_liebiao');
        $this->success("成功","",$html);
        }
    }

    public function search_commentfail()
    {
        $data = db('comment')->alias('c')->join('user u','c.user_id=u.user_id')
        ->join('note n','c.note_id=n.note_id')
        ->field('c.*,u.user_name,n.note_title')->where("comment_status = 0")->select();
        if( empty($data) )
        {
            return 
            ['code'=>0,'msg'=>"查询失败"];
        }
        else
        {
        $this->assign('data',$data);   
        $html = $this->fetch('comment_liebiao');
        $this->success("成功","",$html);
        }
    }
}
